package com.bygf.curve.panel;

import com.bygf.curve.axis.XAxis;
import com.bygf.curve.axis.Y2Axis;
import com.bygf.curve.axis.YAxis;
import com.bygf.curve.interfac.MarkChangeListener;
import java.awt.Color;
import javax.swing.JPanel;

/**
 * 曲线整体面板，包含标题、坐标轴及曲线面板
 *
 * @author sunyupei
 */
public class ChartPanel extends JPanel {

    //曲线标题面板
    private TitlePanel titlePanel = new TitlePanel("标题");
    //X坐标轴
    private XAxis xAxis = new XAxis(this, "横轴");
    //Y坐标轴
    private YAxis yAxis = new YAxis(this, "纵轴1");
    //第二个Y坐标轴
    private Y2Axis y2Axis = new Y2Axis(this, "纵轴2");
    //曲线面板
    private CurvePanel curvePanel = new CurvePanel(this);
    //X轴上的网格线数量
    private int XGridLineCount = 10;
    //Y轴上的网格线数量
    private int YGridLineCount = 10;
    //Y坐标轴面板宽度
    private int YAxisWidth = 60;
    //Y坐标轴2面板宽度
    private int Y2AxisWidth = 30;
    //X坐标轴面板高度
    private int XAxisHeight = 40;
    //总体背景色
    private Color backGroundColor = Color.WHITE;
    //纵轴2的显示隐藏
    private boolean Y2AxisVisible = false;
    //图例的显示隐藏
    private boolean legendVisible = true;
    //标尺操作回调接口
    private MarkChangeListener markListener = null;

    /**
     * 构造方法
     */
    public ChartPanel() {
        this.setLayout(null);
        this.setBackground(backGroundColor);
    }

    /**
     * 构造方法
     *
     * @param title 标题
     * @param xAxisTitle X轴标题
     * @param yAxisTitle Y轴标题
     * @param y2AxisTitle Y2轴标题
     * @param Y2AxisVisible 纵轴2的显示隐藏
     */
    public ChartPanel(String title, String xAxisTitle, String yAxisTitle, String y2AxisTitle, boolean Y2AxisVisible) {
        this.setLayout(null);
        this.titlePanel.setTitle(title);
        this.xAxis.setTitle(xAxisTitle);
        this.yAxis.setTitle(yAxisTitle);
        this.y2Axis.setTitle(y2AxisTitle);
        this.Y2AxisVisible = Y2AxisVisible;
        this.setBackground(backGroundColor);
    }

    /**
     * 初始化各个面板
     */
    private void init() {
        this.removeAll();
        titlePanel.setBounds(0, 0, this.getWidth(), 100);
        this.add(titlePanel);
        xAxis.setBounds(0, this.getHeight() - XAxisHeight, this.getWidth(), XAxisHeight);
        this.add(xAxis);
        yAxis.setBounds(0, titlePanel.getHeight(), YAxisWidth, this.getHeight() - titlePanel.getHeight() - xAxis.getHeight());
        this.add(yAxis);
        if (Y2AxisVisible) {
            Y2AxisWidth = 60;
            y2Axis.setBounds(this.getWidth() - Y2AxisWidth, titlePanel.getHeight(),
                    Y2AxisWidth, this.getHeight() - titlePanel.getHeight() - xAxis.getHeight());
            this.add(y2Axis);
        }
        curvePanel.setBounds(yAxis.getWidth(), titlePanel.getHeight(), this.getWidth() - yAxis.getWidth() - Y2AxisWidth,
                this.getHeight() - titlePanel.getHeight() - xAxis.getHeight());

        this.add(curvePanel);
        this.updateUI();
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        init();
    }

    @Override
    public void setSize(int width, int height) {
        super.setSize(width, height);
        init();
    }

    /**
     * 获取X轴绘制的开始像素位置
     *
     * @return
     */
    public int getXAxisDrawBeginPix() {
        return YAxisWidth;
    }

    /**
     * 获取X轴绘制的结束像素位置
     *
     * @return
     */
    public int getXAxisDrawEndPix() {
        return this.getWidth() - Y2AxisWidth;
    }

    /**
     * @return the xAxis
     */
    public XAxis getXAxis() {
        return xAxis;
    }

    /**
     * @param xAxis the xAxis to set
     */
    public void setXAxis(XAxis xAxis) {
        this.xAxis = xAxis;
        init();
    }

    /**
     * @return the yAxis
     */
    public YAxis getYAxis() {
        return yAxis;
    }

    /**
     * @param yAxis the yAxis to set
     */
    public void setYAxis(YAxis yAxis) {
        this.yAxis = yAxis;
        init();
    }

    /**
     * @return the y2Axis
     */
    public Y2Axis getY2Axis() {
        return y2Axis;
    }

    /**
     * @param y2Axis the y2Axis to set
     */
    public void setY2Axis(Y2Axis y2Axis) {
        this.y2Axis = y2Axis;
        init();
    }

    /**
     * @return the curvePanel
     */
    public CurvePanel getCurvePanel() {
        return curvePanel;
    }

    /**
     * @param curvePanel the curvePanel to set
     */
    public void setCurvePanel(CurvePanel curvePanel) {
        this.curvePanel = curvePanel;
    }

    /**
     * @return the titlePanel
     */
    public TitlePanel getTitlePanel() {
        return titlePanel;
    }

    /**
     * @param titlePanel the titlePanel to set
     */
    public void setTitlePanel(TitlePanel titlePanel) {
        this.titlePanel = titlePanel;
    }

    /**
     * @return the XGridLineCount
     */
    public int getXGridLineCount() {
        return XGridLineCount - 1;
    }

    /**
     * @param XGridLineCount the XGridLineCount to set
     */
    public void setXGridLineCount(int XGridLineCount) {
        if (XGridLineCount < 1) {
            return;
        }
        this.XGridLineCount = XGridLineCount;
    }

    /**
     * @return the YGridLineCount
     */
    public int getYGridLineCount() {
        return YGridLineCount-1;
    }

    /**
     * @param YGridLineCount the YGridLineCount to set
     */
    public void setYGridLineCount(int YGridLineCount) {
        if (YGridLineCount < 1) {
            return;
        }
        this.YGridLineCount = YGridLineCount;
    }

    /**
     * @return the backGroundColor
     */
    public Color getBackGroundColor() {
        return backGroundColor;
    }

    /**
     * @param backGroundColor the backGroundColor to set
     */
    public void setBackGroundColor(Color backGroundColor) {
        this.backGroundColor = backGroundColor;
        this.setBackground(backGroundColor);
    }

    /**
     * @return the markListener
     */
    public MarkChangeListener getMarkListener() {
        return markListener;
    }

    /**
     * @param markListener the markListener to set
     */
    public void setMarkListener(MarkChangeListener markListener) {
        this.markListener = markListener;
    }

    /**
     * @return the bShowY2Axis
     */
    public boolean isY2AxisVisible() {
        return Y2AxisVisible;
    }

    /**
     * @param Y2AxisVisible the bShowY2Axis to set
     */
    public void setY2AxisVisible(boolean Y2AxisVisible) {
        this.Y2AxisVisible = Y2AxisVisible;
        this.init();
    }

    /**
     * @return the legendVisible
     */
    public boolean isLegendVisible() {
        return legendVisible;
    }

    /**
     * @param legendVisible the legendVisible to set
     */
    public void setLegendVisible(boolean legendVisible) {
        this.legendVisible = legendVisible;
    }
}
